package com.xcfw.dorm.seviceimpl.dormcenter;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xcfw.dorm.entity.NumEnum;
import com.xcfw.dorm.entity.ReturnJson;
import com.xcfw.dorm.entity.Student;
import com.xcfw.dorm.exception.ServiceException;
import com.xcfw.dorm.mapper.StudentMapper;
import com.xcfw.dorm.sevice.BaseService;
import com.xcfw.dorm.sevice.ZSGCService;
import com.xcfw.dorm.util.DormUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: Dorm
 * @description: 学生
 * @author: Bruse Queen
 * @create: 2018-04-08 10:26
 **/
@Service("studentServiceImpl")
public class StudentSeriviceImpl extends BaseService implements ZSGCService {
    @Autowired
    private StudentMapper mapper;

    @Override
    public void add(Object o) {
        Student param = (Student) o;

        Integer i = mapper.selectAutoId();
        if (param.getSchoolId() == null) {
            if (null != i) {
                param.setSchoolId(i + 1);
            } else {
                param.setSchoolId(1);
            }
        }

        param.setUuid(DormUtil.getUUID());
        zsgcStatus = mapper.insertSelective(param);
        if (1 != zsgcStatus) {
            throw new ServiceException("新增学生信息失败！");
        }
    }

    @Override
    public void delete(Integer id) {
        zsgcStatus = mapper.deleteByPrimaryKey(id);
        if (1 != zsgcStatus) {
            throw new ServiceException("删除学生信息失败！");
        }
    }

    @Override
    public void update(Object o) {
        Student param = (Student) o;

        Student temp = mapper.selectByPrimaryKey(param.getId());
        if (temp != null) {
            if (param.equals(temp)) {
                throw new ServiceException("您未修改任何信息！");
            }
        }

        zsgcStatus = mapper.updateByPrimaryKeySelective(param);
        if (1 != zsgcStatus) {
            throw new ServiceException("修改学生信息失败！");
        }
    }

    @Override
    public Object select(Object o) {
        Student param = (Student) o;

        String schoolId = DormUtil.setNull(param.getSchoolId());
        String identity = DormUtil.setNull(param.getIdentity());
        String floor = DormUtil.setNull(param.getFloor());
        Integer temp = param.getDormitory() == null ? null : param.getDormitory().intValue();
        String dormitoty = DormUtil.setNull(temp);
        String judgeExamine = DormUtil.setNull(param.getJudgeExamine());

        if ("".equals(param.getName())) {
            param.setName(null);
        }

        ReturnJson result;
        if (param.getPageNum() == null || param.getPageSize() == null) {
            List<Student> list = mapper.selectActionSql(schoolId, param.getName(), null,
                    param.getBuilding(), identity, floor, dormitoty, judgeExamine);
            if (list.size() == 0) {
                throw new ServiceException("暂无相关数据！");
            }
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "学生信息查询成功！", list, list.size());
        } else {
            PageHelper.startPage(param.getPageNum(), param.getPageSize());
            Page<Student> list = (Page<Student>) mapper.selectActionSql(schoolId, param.getName(), null,
                    param.getBuilding(), identity, floor, dormitoty, judgeExamine);
            if (list.size() == 0) {
                throw new ServiceException("暂无相关数据！");
            }
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "学生信息查询成功！", list, list.getTotal());
        }
        return result;
    }

}
